package com.lineage.server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OutErrorMsg {
  private static final Log _log = LogFactory.getLog(OutErrorMsg.class);
  
  public static void put(String className, String string, Throwable t) {
    _log.error(string);
    StringBuilder putInfo = new StringBuilder();
    putInfo.append(String.valueOf(String.valueOf(string)) + "###");
    StackTraceElement[] locations = t.getStackTrace();
    StackTraceElement[] array;
    int length = (array = locations).length;
    int i = 0;
    while (i < length) {
      StackTraceElement stackTraceElement = array[i];
      putInfo.append("   " + stackTraceElement.toString() + "###");
      i++;
    } 
    overOut(className, putInfo);
  }
  
  public static void put(int oid, String string) {
    _log.error(string);
    StringBuilder putInfo = new StringBuilder();
    String nowDate = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date());
    putInfo.append(String.valueOf(String.valueOf(string)) + "/" + nowDate);
    overOut(String.valueOf(oid), putInfo);
  }
  
  private static void overOut(String name, StringBuilder string) {
    try {
      File file = new File("./" + name + ".txt");
      file.createNewFile();
      FileOutputStream outStream = new FileOutputStream(file, true);
      OutputStreamWriter printWriter = new OutputStreamWriter(outStream, "utf-8");
      String[] clientStrAry = string.toString().split("###");
      String[] array;
      int length = (array = clientStrAry).length;
      int i = 0;
      while (i < length) {
        String txt = array[i];
        printWriter.write(txt);
        printWriter.write("\r\n");
        i++;
      } 
      printWriter.write("\r\n");
      printWriter.flush();
      printWriter.close();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
